package com.ra.controller;

import com.ra.pojo.RespBean;
import com.ra.pojo.dto.UserDetailsDTO;
import com.ra.pojo.dto.UserInfoDTO;
import com.ra.pojo.dto.UserLoginParamDTO;
import com.ra.service.IUserAuthService;
import com.ra.utils.BeanCopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * Author : 今晚几点睡_Ada
 * Date: 2023/3/9
 * Time: 11:37
 * 注:
 */

@Api(tags = "登录控制类")
@RestController
public class LoginController {

    @Autowired
    private IUserAuthService userAuthService;


    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public RespBean<?> login(@RequestBody UserLoginParamDTO userLoginParamDTO,
                               HttpServletRequest request) {
        return userAuthService.login(userLoginParamDTO.getUsername(),
                userLoginParamDTO.getPassword(), request);
    }

    @ApiOperation(value = "获取当前用户信息")
    @GetMapping("/users/info")
    public UserInfoDTO getUserInfo(Principal principal) {
        if (null == principal) {
            return null;
        }
        String username = principal.getName();
        UserDetailsDTO user = userAuthService.getUserByUserName(username);

        //UserInfoDTO userInfo = BeanCopyUtil.copyObject(user, UserInfoDTO.class);

        return BeanCopyUtil.copyObject(user,UserInfoDTO.class);
    }
    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public RespBean<?> logout() {
        return RespBean.success("注销成功");
    }

}
